Binary Tree Level Order Traversal II
Given a binary tree, return the bottom-up level order traversal of its nodes' values.
(ie, from left to right, level by level from leaf to root).
For example:
Given binary tree [3,9,20,null,null,15,7],
3
/ \
9 20
/ \
15 7
return its bottom-up level order traversal as:
[ [15,7], [9,20], [3] ]
题目大意:返回树的层次遍历,每一层从左到右,从叶子层到根
题目难度: Medium
import java.util.*;
/**
* Created by gzdaijie on 16/6/7
*/
public class Solution {
public List<List<Integer>> levelOrderBottom(TreeNode root) {
List<List<Integer>> result = new LinkedList<>();
Queue<TreeNode> queue = new LinkedList<>();
if (root == null) return result;
queue.add(root);
while (true) {
int size = queue.size();
if (size == 0) return result;
List<Integer> item = new ArrayList<>();
while (size-- > 0) {
TreeNode top = queue.poll();
item.add(top.val);
if (top.left != null) queue.offer(top.left);
if (top.right != null) queue.offer(top.right);
}
result.add(0,item);
}
}
}